﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using A2.ERP.Models;
using System.Runtime.Remoting.Messaging;
namespace A2.ERP.DAL
{
    public class DbContextFactory
    {
        /// <summary>
        /// 保障在请求过程中只创建一次ef上下文对象
        /// </summary>
        /// <returns></returns>
        public static DbContext CreateDbContext()
        {
            DbContext dbContext = (DbContext)CallContext.GetData("dbContext");

            if (dbContext == null)
            {
                dbContext = new ERP_DBEntities();

                CallContext.SetData("dbContext", dbContext);//保证线程的唯一
            }

            return dbContext;
        }
    }
}
